from rest_framework import serializers
from .models import Cluster, Node, Instance, ResourceUsage

class ResourceUsageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResourceUsage
        fields = ['id', 'instance', 'cpu_usage', 'memory_usage', 'disk_usage', 'timestamp']

class InstanceSerializer(serializers.ModelSerializer):
    usage_records = ResourceUsageSerializer(many=True, read_only=True)
    created_by = serializers.ReadOnlyField(source='created_by.username')

    class Meta:
        model = Instance
        fields = ['id', 'name', 'node', 'status', 'cpu_cores', 'memory_gb', 'disk_gb',
                 'created_at', 'updated_at', 'created_by', 'usage_records']

class NodeSerializer(serializers.ModelSerializer):
    instances = InstanceSerializer(many=True, read_only=True)

    class Meta:
        model = Node
        fields = ['id', 'name', 'cluster', 'ip_address', 'status', 'cpu_cores',
                 'memory_gb', 'disk_gb', 'created_at', 'updated_at', 'instances']

class ClusterSerializer(serializers.ModelSerializer):
    nodes = NodeSerializer(many=True, read_only=True)
    created_by = serializers.ReadOnlyField(source='created_by.username')

    class Meta:
        model = Cluster
        fields = ['id', 'name', 'description', 'created_at', 'updated_at',
                 'created_by', 'nodes'] 